package com.axon.lemon

import grails.converters.JSON

class FavouriteController {
/*
 * 
 *  sql= "select p from Place p left join p.userprofile pu where pu. email =,...."
 * */
	def favouritePage() {
		def currentUser = session.user
		print(currentUser.id)
//		def listFavourite = Favourite.findByUserprofile(currentUser)
//		if(listFavourite != null)
//		print("ok in favouritePage")
//		else print("not ok")
		/*select place.address, place.price, favourite.id 
			from place,user_profile,favourite
			where 
			favourite.userprofile_id = user_profile.id	
			and user_profile.id = 5
			and favourite.place_id = place.id
		 * 
		 * 
		 * */
//		String sql ="select place.address, place.price "+
//						"from place,user_profile,favourite"+
//						+"where "+ 
//						"favourite.userprofile_id = user_profile.id"	
//						+"and user_profile.id = 5"+
//						" and favourite.place_id = place.id"
//		def a= Place.findAll(sql)
//		if(a)print(a)
//		else print("not fine")
		
		def listFavouritePlace = Favourite.findAllByUserprofile(currentUser)
		if(listFavouritePlace!=null) 
			for(int i=0 ;i < listFavouritePlace.size(); i++){
				print (listFavouritePlace[i].place.address+"  "+listFavouritePlace[i].place.description+"  "+listFavouritePlace[i].place.price)
			}
		else print("not fine")
		[listPlaces:listFavouritePlace] 
	}  

	def listPlace (){
		// Get list places
		[places:Place.list()]
	}
	def liked(){
		def yes = "yes";
		def no="not yet"
		def result 
		print("stay at here")
		def placeTemp = Place.get(params.placeid)
		print("address on id place ()"+placeTemp.address)
		def currentUser = session.user
		def isLiked = LikeNumber.findByUserprofileAndPlace(currentUser,placeTemp)		
		if(isLiked) {result = yes 
			print("have a address "+isLiked.place.address+" id place "+params.placeid+" id of NumberOfLike "+isLiked.id)
		}
		else  {result = no
			print("not yet")
		}
		render ([result:result] as JSON)
	}
	def allLike(){
		def placeTemp = Place.get(params.placeid)
		def totalOfLike = placeTemp ? LikeNumber.findAllByPlace(placeTemp) : []
		print(totalOfLike.size()+"  " +params.placeid)
		def tempLike = totalOfLike.size()
		render ([totalOfLike:tempLike] as JSON)
	}
	def isLikeUsersub(){
		def dodgyCount
		print("in ham isLike")
		print(params.testLike+"kiem tra")
		if(params.testLike==true)
		print(params.placeid)
		def currentUser = session.user
		def placeTemp = Place.get(params.placeid)
		print(placeTemp.address)
		def result = new LikeNumber()
		result.place=placeTemp
		result.userprofile=currentUser
		result.save(fush:true)		
		def fakePlace = Place.get(params.placeid)
		dodgyCount = LikeNumber.countByPlace(fakePlace)+1
		print ( "khung dien"+dodgyCount)		 		
		render ([totalLike:dodgyCount] as JSON)
	}
	def isDisLikeUsersub(){
		def dodgyCount
		print("in ham isDisLikeUser")
		print(params.testLike+"kiem tra")
		if(params.testLike==true)
		print(params.placeid)
		def currentUser = session.user
		def placeTemp = Place.get(params.placeid)
		print(placeTemp.address)
		// test lan ni thoy 
		def afterDislike = LikeNumber.findByUserprofileAndPlace(currentUser,placeTemp)
//		dodgyCount = LikeNumber.countByPlace(placeTemp)-1
		afterDislike.delete(flush:true)
		dodgyCount = LikeNumber.countByPlace(placeTemp)
	//	dodgyCount ="xoa roy"
		print("sau khi xoa "+dodgyCount)
		render ([disLike:dodgyCount] as JSON)
	}
	
	def listAllComment(){
		def placeTemp = Place.get(params.placeid)
		print("truoc khi "+placeTemp.address)
		def listOfComment = Comment.findAllByPlace(placeTemp)
		print(listOfComment.size())
		for (int i = 0; i < listOfComment.size(); i++) {
	
			System.out.println(listOfComment[i].content+"  "+listOfComment[i].userprofile.fullName+"  "+listOfComment[i].place.address)
		}		
		render  listOfComment as JSON
	}
	def saveCommentsub(){		
		print("tets")	
		try{
			def CM = params.cm
			def usrID = session.user
			def placeID=params.placeID
			def placeObject = UserProfile.get(placeID)
			def cm1 = new Comment(content: CM)
			def currentPlace = Place.get(placeID) as Place	
		//	def currentUser = UserProfile.get(usrID) as UserProfile
			cm1.place=currentPlace
			cm1.userprofile=usrID
			cm1.save()					
			render ([contentofUSER:CM,userName:usrID.fullName,Addres:currentPlace.address] as JSON);
		}catch(Exception e){
			print(e.getMessage())
		}
		return
	}
	def test(){
		print("sau khi qua test ")
		def kqua ="ok"
		render ([kqua:kqua] as JSON)
	}
	
	def listImage(){
		print("day ne")	;
		 def IDplace=params.placeID
		 print(IDplace);
		 def placeI=Place.get(IDplace)
		 print(placeI)
	
		 def listimage= Image.findAllByPlace(placeI)
		 println("size" + listimage.size())
			 for(int i=0; i<listimage.size() ; i++)
			  {
				 System.out.println(listimage[i].path);
			  }
		 render listimage as JSON;
	}
		def showImage(){
			 def IDplace= params.placeID
			 int index = Integer.parseInt(params.index)
			 def placeI=Place.get(IDplace)
			 def listimage= Image.findAllByPlace(placeI)
			 response.outputStream << listimage.get(index).path
		}
		
		def placeDetail() {
			def IDplace=params.placeID
			//print("placeDetail: "+params.placeID);
			def myPlace=Place.get(IDplace) as Place
			def contact = myPlace.contact as Contact
			def propertytype=myPlace.propertytype as PropertyType
			render([propertytype:propertytype.namePropertyType, address: myPlace.address,price: myPlace.price, description: myPlace.description,bed:myPlace.bed,bath:myPlace.bath,nameContact:contact.nameContact,phoneContact:contact.phoneContact,emailContact:contact.emailContact,image:myPlace.avatar] as JSON)
			
			return
		}
}
